if (BUILD_TESTS AND NOT ANDROID)
    add_subdirectory(test)
endif()

file(GLOB PROFILES_FILES ${CMAKE_CURRENT_LIST_DIR})
source_group("Python Files" FILES ${MERGE_SCRIPT} ${SOLUTION_SCRIPT})

set(PROFILES_FILES_FOR_INSTALL
    VP_KHR_roadmap.json
    VP_LUNARG_minimum_requirements.json
    VP_LUNARG_desktop_baseline.json
    VP_ANDROID_baseline_2021.json
    VP_ANDROID_baseline_2022.json
    VP_ANDROID_15_minimums.json
    VP_ANDROID_16_minimums.json
)

set(PROFILES_FILES_FOR_API_LIBRARY
    "VP_KHR_roadmap.json,VP_LUNARG_minimum_requirements.json,VP_ANDROID_baseline_2021.json,VP_ANDROID_baseline_2022.json,VP_ANDROID_15_minimums.json,VP_ANDROID_16_minimums.json,VP_LUNARG_desktop_baseline.json"
)

set(PROFILES_FILES_FOR_VULKAN_HEADER_DOC
    "VP_KHR_roadmap.json,VP_LUNARG_minimum_requirements.json"
)

set(PROFILES_FILES_FOR_ANDROID_DOC
    "VP_ANDROID_16_minimums.json,VP_ANDROID_15_minimums.json,VP_ANDROID_baseline_2022.json,VP_ANDROID_baseline_2021.json,VP_LUNARG_minimum_requirements.json"
)

set(PROFILE_DESKTOP_MAX_2024_LABEL "LunarG Vulkan Desktop Max 2024 profile")
set(PROFILE_DESKTOP_MAX_2024_DESC "A profile generated by the intersection of a collection of GPUInfo.org device reports to support the latest AMD, Intel and NVIDIA GPUs and drivers.")
set(PROFILE_DESKTOP_MAX_2024_API_VERSION "1.3.244")

add_custom_target(VpCreateDesktopMax2024 ALL
    COMMAND Python3::Interpreter ${MERGE_SCRIPT}
        --registry ${VULKAN_HEADERS_INSTALL_DIR}/${CMAKE_INSTALL_DATADIR}/vulkan/registry/vk.xml
        --input ${CMAKE_CURRENT_LIST_DIR}/VP_LUNARG_desktop_max_2024
        --output-path ${CMAKE_CURRENT_LIST_DIR}/test/data/VP_LUNARG_desktop_max_2024.json
        --output-profile VP_LUNARG_desktop_max_2024
        --profile-label ${PROFILE_DESKTOP_MAX_2024_LABEL}
        --profile-desc ${PROFILE_DESKTOP_MAX_2024_DESC}
        --profile-date 2023-11-01
        --profile-stage BETA
        --profile-api-version ${PROFILE_DESKTOP_MAX_2024_API_VERSION}
    VERBATIM
    SOURCES ${MERGE_SCRIPT} ${PROFILES_FILES}
    DEPENDS ${MERGE_SCRIPT} ${PROFILES_FILES})

set_target_properties(VpCreateDesktopMax2024 PROPERTIES FOLDER "Profiles generator")

add_custom_target(VpCreateDesktopBaseline ALL
    COMMAND Python3::Interpreter ${MERGE_SCRIPT}
        --registry ${VULKAN_HEADERS_INSTALL_DIR}/${CMAKE_INSTALL_DATADIR}/vulkan/registry/vk.xml
        --config ${CMAKE_CURRENT_LIST_DIR}/config/VP_LUNARG_desktop_baseline_config.json
        --output-path ${CMAKE_CURRENT_LIST_DIR}/VP_LUNARG_desktop_baseline.json
        --strip-duplicate-structs
    VERBATIM
    SOURCES ${MERGE_SCRIPT} ${PROFILES_FILES}
    DEPENDS ${MERGE_SCRIPT} ${PROFILES_FILES})

set_target_properties(VpCreateDesktopBaseline PROPERTIES FOLDER "Profiles generator")

add_custom_target(VpGenerated
    COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_SOURCE_DIR}/schema
    COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_SOURCE_DIR}/library/include/vulkan
    COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_SOURCE_DIR}/library/include/vulkan/debug
    COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_SOURCE_DIR}/library/source
    COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_SOURCE_DIR}/library/source/debug
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${VULKAN_HEADERS_INSTALL_DIR}/${CMAKE_INSTALL_DATADIR}/vulkan/registry/profiles ${CMAKE_CURRENT_LIST_DIR}
    COMMAND Python3::Interpreter ${SOLUTION_SCRIPT}
        --api ${API_TYPE}
        --registry ${VULKAN_HEADERS_INSTALL_DIR}/${CMAKE_INSTALL_DATADIR}/vulkan/registry/vk.xml
        --input ${CMAKE_CURRENT_LIST_DIR}
        --input-filenames ${PROFILES_FILES_FOR_API_LIBRARY}
        --output-schema ${PROJECT_SOURCE_DIR}/schema/${PROFILES_SCHEMA_FILENAME}
        --validate
    COMMAND Python3::Interpreter ${SOLUTION_SCRIPT}
        --api ${API_TYPE}
        --registry ${VULKAN_HEADERS_INSTALL_DIR}/${CMAKE_INSTALL_DATADIR}/vulkan/registry/vk.xml
        --input ${CMAKE_CURRENT_LIST_DIR}
        --input-filenames ${PROFILES_FILES_FOR_API_LIBRARY}
        --output-library-inc ${PROJECT_SOURCE_DIR}/library/include/vulkan
        --output-library-src ${PROJECT_SOURCE_DIR}/library/source
        --output-library-filename "vulkan_profiles"
        --config release
    COMMAND Python3::Interpreter ${SOLUTION_SCRIPT}
        --api ${API_TYPE}
        --registry ${VULKAN_HEADERS_INSTALL_DIR}/${CMAKE_INSTALL_DATADIR}/vulkan/registry/vk.xml
        --input ${CMAKE_CURRENT_LIST_DIR}
        --input-filenames ${PROFILES_FILES_FOR_API_LIBRARY}
        --output-library-inc ${PROJECT_SOURCE_DIR}/library/include/vulkan/debug
        --output-library-src ${PROJECT_SOURCE_DIR}/library/source/debug
        --output-library-filename "vulkan_profiles"
        --config debug
    COMMAND Python3::Interpreter ${SOLUTION_SCRIPT}
        --api ${API_TYPE}
        --registry ${VULKAN_HEADERS_INSTALL_DIR}/${CMAKE_INSTALL_DATADIR}/vulkan/registry/vk.xml
        --input ${CMAKE_CURRENT_LIST_DIR}
        --input-filenames ${PROFILES_FILES_FOR_API_LIBRARY}
        --output-doc ${PROJECT_SOURCE_DIR}/PROFILES_ALL.md
    VERBATIM
    SOURCES ${SOLUTION_SCRIPT} ${PROFILES_FILES}
    DEPENDS ${SOLUTION_SCRIPT} ${PROFILES_FILES})

set_target_properties(VpGenerated PROPERTIES FOLDER "Profiles generator")

add_dependencies(VpGenerated VpCreateDesktopBaseline VpCreateDesktopMax2024)

add_custom_target(VpGeneratedVulkanHeaderProfilesDoc
    COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_SOURCE_DIR}/schema
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${VULKAN_HEADERS_INSTALL_DIR}/${CMAKE_INSTALL_DATADIR}/vulkan/registry/profiles ${CMAKE_CURRENT_LIST_DIR}
    COMMAND Python3::Interpreter ${SOLUTION_SCRIPT}
        --api ${API_TYPE}
        --registry ${VULKAN_HEADERS_INSTALL_DIR}/${CMAKE_INSTALL_DATADIR}/vulkan/registry/vk.xml
        --input ${CMAKE_CURRENT_LIST_DIR}
        --input-filenames ${PROFILES_FILES_FOR_VULKAN_HEADER_DOC}
        --output-doc ${PROJECT_SOURCE_DIR}/PROFILES.md
        --validate
    VERBATIM
    SOURCES ${SOLUTION_SCRIPT} ${PROFILES_FILES}
    DEPENDS ${SOLUTION_SCRIPT} ${PROFILES_FILES})

set_target_properties(VpGeneratedVulkanHeaderProfilesDoc PROPERTIES FOLDER "Profiles generator")

add_dependencies(VpGeneratedVulkanHeaderProfilesDoc VpGenerated)

add_custom_target(VpGeneratedAndroidDoc
    COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_SOURCE_DIR}/schema
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${VULKAN_HEADERS_INSTALL_DIR}/${CMAKE_INSTALL_DATADIR}/vulkan/registry/profiles ${CMAKE_CURRENT_LIST_DIR}
    COMMAND Python3::Interpreter ${SOLUTION_SCRIPT}
        --api ${API_TYPE}
        --registry ${VULKAN_HEADERS_INSTALL_DIR}/${CMAKE_INSTALL_DATADIR}/vulkan/registry/vk.xml
        --input ${CMAKE_CURRENT_LIST_DIR}
        --input-filenames ${PROFILES_FILES_FOR_ANDROID_DOC}
        --output-doc ${PROJECT_SOURCE_DIR}/PROFILES_ANDROID.md
        --validate
    VERBATIM
    SOURCES ${SOLUTION_SCRIPT} ${PROFILES_FILES}
    DEPENDS ${SOLUTION_SCRIPT} ${PROFILES_FILES})

set_target_properties(VpGeneratedAndroidDoc PROPERTIES FOLDER "Profiles generator")

add_dependencies(VpGeneratedAndroidDoc VpGenerated)

add_custom_target(VpTestIntersect ALL
    COMMAND Python3::Interpreter ${MERGE_SCRIPT}
        --registry ${VULKAN_HEADERS_INSTALL_DIR}/${CMAKE_INSTALL_DATADIR}/vulkan/registry/vk.xml
        --input ${CMAKE_CURRENT_LIST_DIR}/test/data/VP_LUNARG_test_combine_intersect
        --output-path ${CMAKE_CURRENT_LIST_DIR}/test/data/VP_LUNARG_test_combine_intersect.json
        --output-profile VP_LUNARG_test_combine_intersect
        --mode intersection
    VERBATIM
    SOURCES ${MERGE_SCRIPT} ${PROFILES_FILES}
    DEPENDS ${MERGE_SCRIPT} ${PROFILES_FILES})

set_target_properties(VpTestIntersect PROPERTIES FOLDER "Profiles generator/Tests")

add_dependencies(VpTestIntersect VpGenerated)

add_custom_target(VpTestUnion ALL
    COMMAND Python3::Interpreter ${MERGE_SCRIPT}
        --registry ${VULKAN_HEADERS_INSTALL_DIR}/${CMAKE_INSTALL_DATADIR}/vulkan/registry/vk.xml
        --input ${CMAKE_CURRENT_LIST_DIR}/test/data/VP_LUNARG_test_combine_union
        --output-path ${CMAKE_CURRENT_LIST_DIR}/test/data/VP_LUNARG_test_combine_union.json
        --output-profile VP_LUNARG_test_combine_union
        --mode union
    VERBATIM
    SOURCES ${MERGE_SCRIPT} ${PROFILES_FILES}
    DEPENDS ${MERGE_SCRIPT} ${PROFILES_FILES})

set_target_properties(VpTestUnion PROPERTIES FOLDER "Profiles generator/Tests")

add_dependencies(VpTestUnion VpGenerated)

add_custom_target(VpTestGeneratedName ALL
    COMMAND Python3::Interpreter ${MERGE_SCRIPT}
        --registry ${VULKAN_HEADERS_INSTALL_DIR}/${CMAKE_INSTALL_DATADIR}/vulkan/registry/vk.xml
        --input ${CMAKE_CURRENT_LIST_DIR}/test/data/VP_LUNARG_test_combine_intersect
        --output-path ${CMAKE_CURRENT_LIST_DIR}/test/data/VP_LUNARG_test_generated_name.json
    VERBATIM
    SOURCES ${MERGE_SCRIPT} ${PROFILES_FILES}
    DEPENDS ${MERGE_SCRIPT} ${PROFILES_FILES})

set_target_properties(VpTestGeneratedName PROPERTIES FOLDER "Profiles generator/Tests")

add_dependencies(VpTestGeneratedName VpGenerated)

add_custom_target(VpTestHostImageCopy ALL
    COMMAND Python3::Interpreter ${MERGE_SCRIPT}
        --registry ${VULKAN_HEADERS_INSTALL_DIR}/${CMAKE_INSTALL_DATADIR}/vulkan/registry/vk.xml
        --input ${CMAKE_CURRENT_LIST_DIR}/test/data/VP_LUNARG_test_host_image_copy
        --output-path ${CMAKE_CURRENT_LIST_DIR}/test/data/VP_LUNARG_test_host_image_copy.json
    VERBATIM
    SOURCES ${MERGE_SCRIPT} ${PROFILES_FILES}
    DEPENDS ${MERGE_SCRIPT} ${PROFILES_FILES})

set_target_properties(VpTestHostImageCopy PROPERTIES FOLDER "Profiles generator/Tests")

add_dependencies(VpTestHostImageCopy VpGenerated)

set(json_install_dir "${CMAKE_INSTALL_DATADIR}/vulkan/config/VK_LAYER_KHRONOS_profiles")
if (WIN32)
    set(json_install_dir "Config/VK_LAYER_KHRONOS_profiles")
endif()

install(
    FILES 
        ${PROFILES_FILES_FOR_INSTALL}
    DESTINATION
        ${json_install_dir}
)

install(
    FILES
        ${MERGE_SCRIPT}
        ${SOLUTION_SCRIPT}
        ${PROJECT_SOURCE_DIR}/schema/${PROFILES_SCHEMA_FILENAME}
    DESTINATION
        "${CMAKE_INSTALL_DATADIR}/vulkan/registry"
)
